<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>canvas</title>
    <style>
      #canvas {
        background-color: gray;
      }
    </style>
  </head>
  <body>
    <canvas id="canvas" width="800" height="400"></canvas>
    <script>
      // 获取canvas元素
      const canvas = document.getElementById("canvas");
      // 获取画笔
      const context = canvas.getContext("2d");

      context.beginPath();
      // 绘画
      context.strokeStyle = "pink";
      // 画线 moveTo(x, y) lineTo(x, y)
      context.moveTo(100, 100);
      context.lineTo(200, 200);
      context.lineTo(200, 150);
      context.stroke(); // 闭合

      context.closePath();

      // 画矩形 strokeRect(x, y, width, height)
      context.strokeRect(300, 100, 100, 100);

      context.fillStyle = "yellowgreen";
      context.fillRect(300, 250, 100, 100);

      context.beginPath();
      context.fillStyle = "pink";
      // 画圆 arc(x, y, r, 起始角度, 结束角度, 顺/逆)
      context.arc(500, 200, 50, 0, 2 * Math.PI, true);
      // context.arc(500, 200, 50, 0, Math.PI, true);
      // context.stroke();
      context.fill();
      context.closePath();

      // context.clearRect(0, 0, 800, 400); // 清除画布
      context.font = "20px 宋体";
      context.fillText("尚硅谷", 600, 100);
    </script>
  </body>
</html>
